#include <stdint.h>
#include <stdbool.h>
#include <stdio.h>
#include "wm_hal.h"
#include "wm_cpu.h"
#include "lcd.h"
#include "picture.h"
#include "psram.h"
#include "wm_spi_flash.h"
#include "lvgl.h"
#include "lvgl_task.h"

/**
*   DEMO 0  LCD lvgl、触摸、编码器、软键盘、lvgl组件测试
*   DEMO 1  LCD 颜色填充测试
*   DEMO 2  LCD 图片转c数组测试
*   DEMO 3  spi flash 读写测试
*   DEMO 4  psram 测试
*   DEMO 5  LCD 串口工具将图片转二进制文件，通过xmodem下载到spi flash显示
*/
#define RUN_DEMO_NUM  (0)

#if (RUN_DEMO_NUM == 5)
#include "button/multi_button.h"
#include "xmodem/xmodem.h"
#include "spiflash.h"
volatile uint8_t xmodem_flag = 0;
struct Button btn1;
void BTN1_LONG_PRESS_START_Handler(void* btn)
{
    xmodem_flag = 1;
}
#endif

int main(void)
{
	SystemClock_Config(CPU_CLK_240M);
	printf("             \\\\\\|///\n");
	printf("           \\\\  .-.-  //\n");
	printf(".           (  .@.@  )  \n");
	printf("+-------oOOo-----(_)-----oOOo---------+\n\n");
	printf(" ---> Compile "__DATE__", "__TIME__"\n");
	printf(" ---> %s\r\n",__func__);
    printf(" ---> SDK: %s\n", VER);
	printf("\n+---------------------Oooo------------+\n");

#if  (RUN_DEMO_NUM == 0) // 使能lvgl任务
    printf("---> RUN DEMO [%d] LVGL \n", RUN_DEMO_NUM);
    lvgl_task_start();
#elif(RUN_DEMO_NUM == 1) // 颜色填充测试
    printf("---> RUN DEMO [%d] FULL COLORS \n", RUN_DEMO_NUM);
    bsp_lcd_init();
    uint8_t index = 0;
    uint16_t buff[]={RED,GREEN,BLUE,BRED,YELLOW,RED,GRAY};
    while(1)
    {
        if(++index>=7)
        {
            index = 0;
        }
        // lcd_clear(RED);
        // lcd_clear(0b1111100000000000);
        lcd_clear(buff[index]);
        HAL_Delay(1000);
        
    }
#elif(RUN_DEMO_NUM == 2) // 图片数组测试
    printf("---> RUN DEMO [%d] IMAGE C ARRAY \n", RUN_DEMO_NUM);
    bsp_lcd_init();
    while(1){
        lcd_clear(BLACK);
        lcd_full_picture(320, 480, (uint8_t *)image_labi_320x480);
        HAL_Delay(1000);
        lcd_full_picture(480, 272, (uint8_t *)image_bluesky_272x480);
        HAL_Delay(1000);
        lcd_full_picture(240, 320, (uint8_t *)image_flowers_240x320);
        HAL_Delay(1000);
        lcd_full_picture(48, 48, (uint8_t *)image_red_48x48);
        HAL_Delay(1000);
    }
#elif(RUN_DEMO_NUM == 3) // spi flash test
    printf("---> RUN DEMO [%d] SPI FLASH TEST \n", RUN_DEMO_NUM);
    uint8_t buf[64]={0};
    HAL_FLASH_Read(0x96000, buf, 64);
    for(uint16_t i = 0; i < 64; i++){
        printf("%02X ", buf[i]);
    }
    printf("\n");
    uint8_t data = buf[0] + 1;
    memset(buf, data, 64);
    HAL_FLASH_Write(0x96000, buf, 64);
    for(uint16_t i = 0; i < 64; i++){
        printf("%02X ", buf[i]);
    }
    printf("\n");
#elif(RUN_DEMO_NUM == 4) // psram test
    printf("---> RUN DEMO [%d] SPI PSRAM \n", RUN_DEMO_NUM);
    char *test = dram_heap_malloc(128);
    strcpy(test,"hello world!!!\r\n");
    printf("%s",test);
    dram_heap_free(test);
#elif(RUN_DEMO_NUM == 5) // xmodem下载图片显示demo
    printf("---> RUN DEMO [%d] XMODEM TRANSMISSION IMAGE DISPLAY \n", RUN_DEMO_NUM);
    button_init(&btn1, GPIOA, GPIO_PIN_0, 0);
    button_attach(&btn1, LONG_PRESS_START, BTN1_LONG_PRESS_START_Handler);
    button_start(&btn1);
    bsp_lcd_init();
	while(1)
	{
		if(xmodem_flag == 1)
		{
			HAL_Delay(500);
			xmodem_download();
		}
		else
		{
			get_image_list_show();
		}
	}
#endif

    while (1)
	{
		HAL_Delay(1000);
	}
	
	return 0;
}

void Error_Handler(void)
{
	while (1)
	{
	}
}

void assert_failed(uint8_t *file, uint32_t line)
{
	printf("Wrong parameters value: file %s on line %d\r\n", file, line);
}